package com.cdshangpai.utils.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cdshangpai.utils.db.beans.Aboutus;

import java.util.ArrayList;
import java.util.List;

public class AboutusDb {
    SQLiteDatabase db=DatabaseHelper.getDb();
    String tablename="t_aboutus";

    public long insert(Aboutus aboutus)
    {
        ContentValues values=aboutus.getValues();
        return db.insert(tablename,null,values);
    }

    public void update(Aboutus aboutus)
    {
        db.update(tablename,aboutus.getValues(),null,null);
    }

    public void clear()
    {
        String sql="delete from "+tablename;
        db.execSQL(sql);
    }

    public List<Aboutus> query()
    {
        List<Aboutus> result=new ArrayList<>();
        Aboutus aboutus=queryBean();
        if(aboutus!=null)
        {
            result.add(aboutus);
        }
        return result;
    }

    public Aboutus queryBean()
    {
        String sql="select * from "+tablename;
        Cursor cursor=db.rawQuery(sql,null);
        cursor.moveToFirst();
        if(!cursor.isAfterLast())
        {
            Aboutus aboutus=cToBean(cursor);
            cursor.close();
            return aboutus;
        }
        cursor.close();
        return null;
    }

    private Aboutus cToBean(Cursor c)
    {
        Aboutus aboutus=new Aboutus();
        aboutus.id=c.getInt(c.getColumnIndex("id"));
        aboutus.content=c.getString(c.getColumnIndex("content"));
        aboutus.area=c.getString(c.getColumnIndex("area"));
        aboutus.photos=c.getString(c.getColumnIndex("photos"));
        aboutus.homephotos=c.getString(c.getColumnIndex("homephotos"));

        aboutus.ideaphotos=c.getString(c.getColumnIndex("ideaphotos"));
        aboutus.servicephotos=c.getString(c.getColumnIndex("servicephotos"));
        aboutus.advantagephotos=c.getString(c.getColumnIndex("advantagephotos"));
        aboutus.casephotos=c.getString(c.getColumnIndex("casephotos"));
        aboutus.chargesphotos=c.getString(c.getColumnIndex("chargesphotos"));
        aboutus.promotionphotos=c.getString(c.getColumnIndex("promotionphotos"));
        aboutus.letterphotos=c.getString(c.getColumnIndex("letterphotos"));
        aboutus.processphotos=c.getString(c.getColumnIndex("processphotos"));
        aboutus.documentphotos=c.getString(c.getColumnIndex("documentphotos"));

        aboutus.version=c.getString(c.getColumnIndex("version"));
        return aboutus;
    }
}
